// pages/bind/bind.js

import API from "../../data/api";
import Util from "../../utils/util";

const App = getApp();
const Config = App.Config;

Page({
  data: {
    Stnumber: '',
    Stpassword: '',
    openId: null,
    HOSTIMG: Config.HOSTIMG,
    windowWidth: App.globalData.systemInfo.windowWidth,
    windowHeight: App.globalData.systemInfo.windowHeight
  },
  onShow: function (options) {

    var that = this

    if (App.globalData.openId == '' || App.globalData.openId == null) {
      Util.UI.loading(1000, '授权失败,请退出小程序重新登录');
      setTimeout(function () {
        wx.switchTab({
          url: '../index/index',
        })
      }, 1000)
    }

    App.Util.getStNumberPassword(App.globalData.openId) // 获取学号密码
      .then((res) => {
        if (res.data.number != "") {
          that.setData({
            Stnumber: res.data.number,
            Stpassword: res.data.password,
            openId: App.globalData.openId
          });
        }
      })
  },

  //获取输入框的数据
  numberInput: function (event) {
    this.setData({ Stnumber: event.detail.value });
  },
  passwordInput: function (event) {
    this.setData({ Stpassword: event.detail.value });
  },
  //点击确认按钮，提交数据
  confirmBtnClick: function () {
    var that = this;
    // console.log(that.data.number);
    //调用远端
    // console.log(that.data.number);
    if (that.data.Stnumber == "" || that.data.Stpassword == "") {
      wx.showModal({
        title: '提示',
        content: '学号和密码不能为空哦哦~',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
    } else {
      API.bind(that.data.openId, that.data.Stnumber, that.data.Stpassword)
        .then((res) => {
          // console.log(res);
          if (res.data.error == Config.ERR.OK) {
            Util.UI.toast('提交成功');
            setTimeout(function () {
              wx.switchTab({
                url: '../index/index',
              })
            }, 1000)
          } else if (res.data.error == Config.ERR.PW) {
            Util.UI.loading(1000, '账号或者密码错误');
          }
        });
    }
  },

})